layui.use(['layer', 'form', 'table', 'ztree', 'laydate', 'admin', 'ax', 'func', 'tree', 'util'], function () {
    var layer = layui.layer;
    var form = layui.form;
    var table = layui.table;
    var $ZTree = layui.ztree;
    var $ax = layui.ax;
    var laydate = layui.laydate;
    var admin = layui.admin;
    var func = layui.func;
    var tree = layui.tree;
    var util = layui.util;

    /**
     * 资讯管理--资讯列表
     */
    var base = {
        tableId: "baseTable"    //表格id
    };
    
    var bases = {
            tableId: "baseTables"    //表格id
        };

    //渲染时间选择框
    laydate.render({
        elem: '#timeLimit',
        range: true,
        max: Feng.currentDate()
    });
    
    //渲染时间选择框
    laydate.render({
    	elem: '#timeLimits',
    	range: true,
    	max: Feng.currentDate()
    });
    
    //渲染时间选择框
    laydate.render({
    	elem: '#timeLimitss',
    	range: true,
    	max: Feng.currentDate()
    });

    /**
     * 初始化表格的列
     */
    base.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'information_title', align: "center", sort: true, title: '标题'},
            {field: 'pic', align: "center", sort: true, title: '首图',templet:'<div><img lay-filter="imgck" id="imgck" style="height:100px;width:100px;" src="{{d.pic}}"></div>'},
            {field: 'dzsl', align: "center", sort: true, title: '点赞数量'},
            {field: 'scsl', align: "center", sort: true, title: '收藏数量'},
            {field: 'zfsl', align: "center", sort: true, title: '分享数量'},
            {field: 'browse', align: "center", sort: true, title: '浏览人数'},
            {field: 'releaseby', align: "center", sort: true, title: '发布者'},,
            {field: 'release_time', align: "center", sort: true, title: '发布时间'},
            {field: 'create_time', align: "center", sort: true, title: '申请时间'},
            {fixed: 'right', toolbar: '#toolBtn', title: '操作', align: 'center', width: 130}
        ]];
    };
    
    bases.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'information_title', align: "center", sort: true, title: '标题'},
            {field: 'pic', align: "center", sort: true, title: '首图',templet:'<div><img lay-filter="imgck" id="imgck" style="height:100px;width:100px;" src="{{d.pic}}"></div>'},
            {field: 'dzsl', align: "center", sort: true, title: '点赞数量'},
            {field: 'scsl', align: "center", sort: true, title: '收藏数量'},
            {field: 'zfsl', align: "center", sort: true, title: '分享数量'},
            {field: 'browse', align: "center", sort: true, title: '浏览人数'},
            {field: 'releaseby', align: "center", sort: true, title: '发布者'},,
            {field: 'status', align: "center", sort: true, title: '状态',templet:function(d) {
            	var str = '';
            	if(d.status == 0){
            		str = '<p class="layui-btn layui-btn-yes-b layui-btn-xs">新创</p>';
            	}else if(d.status == 1){
            		str = '<p class="layui-btn layui-btn-yes-b layui-btn-xs">已上架</p>';
            	}else{
            		str = '<p class="layui-btn layui-btn-yes-a layui-btn-xs">已下架</p>';
            	}
            	return str;
            }},
            {field: 'release_time', align: "center", sort: true, title: '发布时间'},
            {field: 'create_time', align: "center", sort: true, title: '申请时间'},
            {fixed: 'right', toolbar: '#toolBtns', title: '操作', align: 'center', width: 130}
        ]];
    };
    
    //绑定事件
    $(document).on('click', '#adopt', function(data) {
        var id = $(this).attr("data-id");
        var status = $(this).attr("name");
        var title ="";
        if(status == '1'){
        	title ="您确认发布该条资讯吗?"; 
        }else{
        	title ="您确认下架该条资讯吗?";
        }
    		layer.confirm(title, function() {
    			var data = {
    					informationId : id,
    					status : status
    				};
    			//调用ajax局部启动
    			$.ajax({
    				url : Feng.ctxPath + "/modular/information/update",
    				type : "POST",
    				data :JSON.stringify(data),
    				dataType : "json",
    				contentType: "application/json;charset=UTF-8",
    				async : false,
    				cache : false,
    				success : function(obj) {
    					if (obj != null) {
    						if (obj.success) {
    							layer.msg("操作成功");
    						} else {
    							layer.msg("操作失败");
    						}
    					}
    				}
    			});
    		    if(status == '1'){
    		    	tableResults.reload(); 
    		    }else{
    		    	tableResult.reload();
    		    }
    			setTimeout("layer.close(layer.index);",2000);
    		});
    });
    
    //绑定事件
    $(document).on('click', '#delbtn', function(data) {
        var informationId = $(this).attr("data-id");
    	layer.confirm("您确认删除该条资讯吗?", function() {
			//调用ajax局部启动
			$.ajax({
				url : Feng.ctxPath + "/modular/information/delete/"+informationId,
				type : "POST",
				contentType: "application/json;charset=UTF-8",
				async : false,
				cache : false,
				success : function(obj) {
					if (obj != null) {
						if (obj.success) {
							layer.msg("操作成功");
						} else {
							layer.msg("操作失败");
						}
					}
				}
			});
			tableResults.reload();
			setTimeout("layer.close(layer.index);",2000);
		});
    	
    });
    
	 // 渲染表格
    var tableResults = table.render({
        title: "未发布列表",
        elem: '#' + bases.tableId,
        url: Feng.ctxPath + '/modular/information/lists',
        page: true,
        height: "full-98",
        cellMinWidth: 100,
        cols: bases.initColumn()
    });
    
    $(document).on('click', '#adopts', function(data) {
        var informationId = $(this).attr("data-id");
		 func.open({
	            type: 2,
	            shadeClose: true,
	            shade: 0.5,
	            area: '300px',
	            title: '查看',
	            maxmin: true, //开启最大化最小化按钮
	            content: Feng.ctxPath + "/modular/information/details/" + informationId,
	            tableId: base.tableId
	        });
    });
    
    $(document).on('click', '#imgck', function(data) {
    	var imgHtml = "<img src='" + $(this).attr("src") + "' />";  
    	layer.open({ 
    		type: 1, 
    		shade: false,
    		title: false,
    		area: [790+'px', 500+'px'],
    		content: imgHtml,
    		cancel: function () {
    			
    		}
	    });
    });
    
    //绑定事件
    $(document).on('click', '#btn', function(data) {
    	$(this).addClass("layui-btn-group-yans").removeClass("layui-btn-group-nans");
    	$("#btns").addClass("layui-btn-group-nans").removeClass("layui-btn-group-yans");
    	$("#diva").show();
    	$("#divb").hide();
    	$("#taba").show();
    	$("#tabb").hide();
    	tableResult.reload();
    });
    
    //绑定事件
    $(document).on('click', '#btns', function(data) {
    	$(this).addClass("layui-btn-group-yans").removeClass("layui-btn-group-nans");
    	$("#btn").addClass("layui-btn-group-nans").removeClass("layui-btn-group-yans");
    	$("#divb").show();
    	$("#diva").hide();
    	$("#tabb").show();
    	$("#taba").hide();
    	tableResults.reload();
    });


    /**
     * 点击查询按钮
     */
    base.search = function () {
        var queryData = {};
        queryData['keyword'] = $("#keyword").val();
        queryData['timeLimit'] = $("#timeLimit").val();
        queryData['timeLimits'] = $("#timeLimits").val();
        table.reload(base.tableId, {
            where: queryData, page: {curr: 1}
        });
    };
    
    /**
     * 点击查询按钮
     */
    bases.searchs = function () {
        var queryData = {};
        queryData['keyword'] = $("#keywords").val();
        queryData['timeLimit'] = $("#timeLimitss").val();
        queryData['status'] = $("#status").find("option:selected").val();
        table.reload(bases.tableId, {
            where: queryData, page: {curr: 1}
        });
    };


    /**
     * 点击查看通知
     *
     * @param data 点击按钮时候的行数据
     */
    base.onQueryDetails = function (data) {
        func.open({
            type: 2,
            shadeClose: true,
            shade: 0.5,
            area: '300px',
            title: '查看详情',
            maxmin: true, //开启最大化最小化按钮
            content: Feng.ctxPath + "/information/details/" + data.user_id,
            tableId: base.tableId
        });
    };

    // 搜索按钮点击事件
    $('#btnAdd').click(function () {
    	base.add();
    });
    
    // 搜索按钮点击事件
    $('#btnAdds').click(function () {
    	base.add();
    });
    
    base.add = function () {
        func.open({
            type: 2,
            shadeClose: true,
            shade: 0.5,
            area: '300px',
            title: '新增',
            maxmin: true, //开启最大化最小化按钮
            content: Feng.ctxPath + "/modular/information/add",
            tableId: base.tableId
        });
    };
    
    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        base.search();
    });
    
    // 搜索按钮点击事件
    $('#btnSearchs').click(function () {
        bases.searchs();
    });


    // 导出excel
    $('#btnExp').click(function () {
        base.exportExcel();
    });
    
    // 导出excel
    $('#btnExps').click(function () {
    	bases.exportExcels();
    });

    // 渲染表格
    var tableResult = table.render({
        title: "已发布列表",
        elem: '#' + base.tableId,
        url: Feng.ctxPath + '/modular/information/list',
        page: true,
        height: "full-98",
        cellMinWidth: 100,
        cols: base.initColumn()
    });
    
    base.exportExcel = function () {
        var checkRows = table.checkStatus(base.tableId);

        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            // table.exportFile([' 表头',' 表头',' 表头',' 表头',' 表头',' 表头'], cons , 'xls',name);
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };
    
    bases.exportExcels = function () {
        var checkRows = table.checkStatus(bases.tableId);

        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            // table.exportFile([' 表头',' 表头',' 表头',' 表头',' 表头',' 表头'], cons , 'xls',name);
            table.exportFile(tableResults.config.id, checkRows.data, 'xls');
        }
    };
});

